package com.example.mvp_demo2.view.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.example.mvp_demo2.R;
import com.example.mvp_demo2.view.bean.User;
import com.example.mvp_demo2.view.presenter.UserLoginPresenter;

public class MainActivity extends AppCompatActivity implements IUserLoginView {

    private EditText mUsername;
    private EditText mPassworld;
    private Button mLogin;
    private Button mClear;
    private ProgressBar mLoading;

    private User mUser = new User();
    private UserLoginPresenter mUserLoginPresenter = new UserLoginPresenter(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initview();

    }

    private void initview() {
        mUsername = (EditText) findViewById(R.id.username);
        mPassworld = (EditText) findViewById(R.id.psw);

        mLogin = (Button) findViewById(R.id.login);
        mClear = (Button) findViewById(R.id.clear);

        mLoading = (ProgressBar) findViewById(R.id.loading);

        mLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mUserLoginPresenter.login();
            }
        });

        mClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mUserLoginPresenter.clear();
            }
        });
    }

    @Override
    public String getUserName() {
        return mUsername.getText().toString();
    }

    @Override
    public String getPassWorld() {
        return mPassworld.getText().toString();
    }

    @Override
    public void clearUserName() {
        mUsername.setText("");
    }

    @Override
    public void ClearPassWorld() {
        mPassworld.setText("");
    }

    @Override
    public void showLoading() {
        mLoading.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideLoading() {
        mLoading.setVisibility(View.GONE);
    }

    @Override
    public void toMainActivity(User user) {
        String username = user.getUsername();
        String passworld = user.getPassworld();
        if ("eye".equals(username) && "123".equals(passworld))
        Toast.makeText(this,username+"--"+passworld,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showFailedError() {
        Toast.makeText(this,"登录失败",Toast.LENGTH_SHORT).show();
    }
}
